package com.moyu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moyu.common.core.domain.R;
import com.moyu.common.core.enums.ResultCode;
import com.moyu.system.domain.SysUser;
import com.moyu.system.mapper.SysUserMapper;
import com.moyu.system.service.ISysUserService;
import com.moyu.system.utils.BCryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 赵彦博
 * @Date 2024/11/16 20:22
 */
@Service
public class SysUserServcieImpl implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public R<Void> login(String userAccount, String password) {
        // 通过账号去数据库中查询，对应的用户信息
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper.select(SysUser::getPassword).eq(SysUser::getUserAccount, userAccount));
//        R loginResult = new R();
        if (sysUser == null) {
//            loginResult.setCode(ResultCode.AILED_USER_EXISTS.getCode());
//            loginResult.setMsg(ResultCode.AILED_USER_EXISTS.getMsg());
            return R.fail(ResultCode.AILED_USER_EXISTS);
        }
        if (BCryptUtils.matchesPassword(password,sysUser.getPassword())) {
//            loginResult.setCode(ResultCode.SUCCESS.getCode());
//            loginResult.setMsg(ResultCode.SUCCESS.getMsg());
            return R.ok();
        }
//        loginResult.setCode(ResultCode.FAILED_LOGIN.getCode());
//        loginResult.setMsg(ResultCode.FAILED_LOGIN.getMsg());
        return R.fail(ResultCode.FAILED_LOGIN);
    }
}
